﻿using System;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace NczControlLibrary.Controls {
    [TemplatePart(Name = "hbClose", Type = typeof(HyperlinkButton)),
    TemplatePart(Name = "hbConfirm", Type = typeof(HyperlinkButton)),]
    public class SearchBox : Control {
        enum State {
            woking, normal
        }
        public SearchBox() {
            DefaultStyleKey = typeof(SearchBox);
        }
        HyperlinkButton hbClose, hbConfirm;
        public override void OnApplyTemplate() {
            hbClose = GetTemplateChild("hbClose") as HyperlinkButton;
            hbConfirm = GetTemplateChild("hbConfirm") as HyperlinkButton;
            //
            hbConfirm.Click += hbConfirm_Click;
            hbClose.Click += hbClose_Click;
            base.OnApplyTemplate();
        }
        System.Threading.SynchronizationContext sc = System.Threading.SynchronizationContext.Current;
        void hbConfirm_Click(object sender, RoutedEventArgs e) {
            Task.Factory.StartNew(() => {
                Task.Factory.StartNew(() => {
                    sc.Send(soap => {
                        SearchWoking();
                    }, null);
                });
                sc.Post(soap => {
                    if (Search != null) {
                        Search.Invoke(this, EventArgs.Empty);
                    }
                    if (SearchCommand != null) {
                        SearchCommand.Execute(Text);
                    }
                    //System.Threading.Thread.Sleep(5000);
                }, null);
            }).ContinueWith(x => {
                sc.Send(soap => {
                    //SearchComplated();
                }, null);
            });
        }
        void SearchWoking() {
            VisualStateManager.GoToState(this, State.woking.ToString(), false);
        }
        void SearchComplated() {
            VisualStateManager.GoToState(this, State.normal.ToString(), false);
        }
        public event EventHandler Search;
        void hbClose_Click(object sender, RoutedEventArgs e) {
            Text = string.Empty;
        }
        #region dependency properties
        public static readonly DependencyProperty SearchCommandProperty = DependencyProperty.Register("SearchCommand", typeof(ICommand), typeof(SearchBox), new PropertyMetadata(default(ICommand), (o, k) => {
        }));
        public ICommand SearchCommand { get { return (ICommand)this.GetValue(SearchCommandProperty); } set { this.SetValue(SearchCommandProperty, value); } }
        public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke", typeof(Brush), typeof(SearchBox), new PropertyMetadata(default(Brush), (o, k) => {
        }));
        public Brush Stroke { get { return (Brush)this.GetValue(StrokeProperty); } set { this.SetValue(StrokeProperty, value); } }
        public static readonly DependencyProperty IconHeightProperty = DependencyProperty.Register("IconHeight", typeof(double), typeof(SearchBox), new PropertyMetadata(20.0d, (o, k) => {
        }));
        public double IconHeight { get { return (double)this.GetValue(IconHeightProperty); } set { this.SetValue(IconHeightProperty, value); } }
        public static readonly DependencyProperty IconWidthProperty = DependencyProperty.Register("IconWidth", typeof(double), typeof(SearchBox), new PropertyMetadata(20.0d, (o, k) => {
        }));
        public double IconWidth { get { return (double)this.GetValue(IconWidthProperty); } set { this.SetValue(IconWidthProperty, value); } }
        public static readonly DependencyProperty FillProperty = DependencyProperty.Register("Fill", typeof(Brush), typeof(SearchBox), new PropertyMetadata(new SolidColorBrush(Colors.Gray), (o, k) => {
        }));
        public Brush Fill { get { return (Brush)this.GetValue(FillProperty); } set { this.SetValue(FillProperty, value); } }
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(SearchBox), new PropertyMetadata(default(string), (o, k) => {
        }));
        public string Text { get { return (string)this.GetValue(TextProperty); } set { this.SetValue(TextProperty, value); } }
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(double), typeof(Gborder), new PropertyMetadata(default(double), (o, k) => {
        }));
        public double CornerRadius { get { return (double)this.GetValue(CornerRadiusProperty); } set { this.SetValue(CornerRadiusProperty, value); } }
        #endregion
    }
}
